#pragma once 

#include "lengthCodec.h"
namespace TinyRPC {


std::string LengthCodec::decode(BufferPtr inputBuffer){
   int len;
   std::string len_str = inputBuffer->retrieve(sizeof(len));
   len_str.copy((char*) &len, sizeof(len));
   if(inputBuffer->getReadableLen() >= len + sizeof(len)){
      inputBuffer->addReadIndex(sizeof(len));
      return inputBuffer->read(len);
   }else{
      return "";
   }
}

std::string LengthCodec::encode(std::string& out){
   int out_len = out.size();
   out.insert(0, std::string((char*)&out_len,sizeof(out_len)));
   return out;
}


}
